/**
 * 
 */
package org.myontology.controller.impl;

import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

import model.Vocabulary;

import org.myontology.controller.AbstractAction;
import org.myontology.controller.Action;
import org.myontology.controller.AttributeHolder;
import org.myontology.controller.AttributeLevel;
import org.myontology.controller.SesameSession;
import org.myontology.controller.ValueHandler;
import org.myontology.controller.rules.SimpleRuleEngine;
import org.myontology.controller.rules.impl.Actions;
import org.openrdf.model.URI;
import org.openrdf.model.impl.URIImpl;

import controller.sesameDataConnector.AbstractDataConnector;

/**
 *  @author <a href="michael.hackl@sti2.at">Michael Hackl</a>
 * Project: Myontology - www.myontology.org
 * STI
 */
public class DiscussionAction extends AbstractAction implements Action {

	public DiscussionAction() {
		super();
	}
	
	public DiscussionAction(SimpleRuleEngine rules,SesameSession session) {
		super();
		this.rules = rules;
		this.session = session;
		
	}
	
	private SimpleRuleEngine rules;
	private SesameSession session;
	
	
	public static final ValueHandler<URI> URI = new ValueHandler<URI>(AttributeLevel.SESSION,DiscussionAction.class);
	public static final ValueHandler<Boolean> WRITEABLE = new ValueHandler<Boolean>(AttributeLevel.REQUEST ,DiscussionAction.class);
	public static final ValueHandler<List<Discussion>> THREAD = new ValueHandler<List<Discussion>>(AttributeLevel.REQUEST,DiscussionAction.class);
	
	public static final ValueHandler<Map<String, String>> SKYPE_NAMES = new ValueHandler<Map<String,String>>(AttributeLevel.REQUEST,DiscussionAction.class);
	
	/* (non-Javadoc)
	 * @see org.myontology.controller.Action#execute(org.myontology.controller.AttributeHolder)
	 */
	public void execute(AttributeHolder attributes) throws Exception {
		if (attributes.getRequestParameter("id")!=null && !attributes.getRequestParameter("id").equals("")) {
			URI uri = AbstractDataConnector.getURI( attributes.getRequestParameter("id") );
			URI.setValue(attributes, uri);
		}
		if (rules.check(Actions.DISCUSS, attributes,session)) {
			WRITEABLE.setValue(attributes, true);
		}
		else {
			WRITEABLE.setValue(attributes, false);
		}
		
		loadThread(attributes);
	}

	/**
	 * @param attributes
	 * @throws Exception
	 */
	private void loadThread(AttributeHolder attributes) throws Exception {
		List<String> discussion = session.loadSubjectsByObject(Vocabulary.ISDISCUSSIONFOR, URI.getValue(attributes));
		List<Discussion> thread = new ArrayList<Discussion>();
		Map<String,String> skypeMap = new HashMap<String, String>();
		for (Iterator<String> it = discussion.iterator();it.hasNext();) {
			String id = it.next();
			Discussion d = new Discussion();
			d.id.addValue(id.substring(id.lastIndexOf('/')+1));
			session.loadUser(d);
			
			thread.add(d);
			
			String username = d.author.getValueString();
			if (username!=null && !username.equals("") && !skypeMap.containsKey(username)) {
				String skypeName = session.loadSingleObject(new URIImpl(Vocabulary.USER_NAMESPACE+username), Vocabulary.HASSKYPE);
				if (skypeName!=null) {
					skypeMap.put(username, skypeName);
				}
			}
			
		}
		
		Collections.sort(thread,new Comparator<Discussion>() {

			public int compare(Discussion o1, Discussion o2) {
				
				return (int)(Long.parseLong(o1.id.getValueString()) - Long.parseLong(o2.id.getValueString()));
			}
			
		});
		SKYPE_NAMES.setValue(attributes, skypeMap);
		THREAD.setValue(attributes, thread);
	}

}
